//
//  MapView.swift
//  swiftStudy
//
//  Created by ext.zhangjunfei on 2021/7/6.
//

import SwiftUI
import MapKit

struct IdentifiableMapPin : Identifiable {
    
    var id: Int
    var mapPin: MapPin
    
}

struct MapView: View {
    
    @Environment(\.presentationMode) var environment
    
    @State private var region = MKCoordinateRegion(
           center: CLLocationCoordinate2D(latitude: 38.626776, longitude: 116.253066),
           span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
       )
    
    @State var mapPins : [IdentifiableMapPin] = []
    
    
    
    var body: some View {
        
        ZStack(alignment: Alignment(horizontal: .leading, vertical: .top)){

            ///地图
            Map(coordinateRegion: $region, annotationItems: mapPins) { identifiable in
                
                identifiable.mapPin
            }.onAppear {
                
                //当地图加载完成后添加图钉
                let newPin = MapPin(coordinate: CLLocationCoordinate2D(latitude: 38.626776, longitude: 116.253066))
                
                mapPins.append(IdentifiableMapPin(id: mapPins.count, mapPin: newPin))
                
            }
            
            ///返回按钮
            ZStack{
                Button(
                    action: {self.environment.wrappedValue.dismiss()},
                    label: {
                        Image(systemName: "arrow.backward")
                        
                    })
                    .frame(width: 35, height: 35)
                    .background(Color.white)
                    .foregroundColor(.black)
                    .cornerRadius(90)
                    .shadow(radius: 10)
                
            }.padding(EdgeInsets(top: 50, leading: 20, bottom: 0, trailing: 0))
           
        }
       
    }
}

struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView()
    }
}
